﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MUD.Lib.Utils
{
    public static class Strings
    {
        #region String Constants
        // communication
        public static string Broadcast = "{0} broadcasts: {1}";

        // error messages
        public static string NoCommand = "%^RED%^%^BOLD%^\r\nCommand not found!\r\n%^RESET%^";
        public static string VoidEnvironment = "You float in a sea of nothingness.";
        public static string ObjectNotFound = "I don't see that, what are you trying to find again?";
        #endregion


        #region String Extension Methods

        /// <summary>
        /// This extension method returns a slice of a space-delimited string, or sentence,
        /// from the first word to the optional last word.
        /// </summary>
        /// <param name="original"></param>
        /// <param name="firstWord">Which word to start with.</param>
        /// <param name="lastWord">(Optional) Last word to include. Leave null for all words following the first.</param>
        /// <returns>Sliced string.</returns>
        public static string Slice(this string original, int firstWord, int? lastWord)
        {
            string[] buffer = original.Split(' ');
            StringBuilder sb = new StringBuilder(buffer.Length);
            for (int i = firstWord; i <= (lastWord==null ? buffer.Length - 1 : (int)lastWord); i++)
            {
                sb.Append(buffer[i] + " ");
            }
            return sb.ToString().Trim();
        }

        /// <summary>
        /// Grabs the Nth word in a space-delimited string or sentence.
        /// </summary>
        /// <param name="original"></param>
        /// <param name="word">Which word?</param>
        /// <returns>The string containing that word.</returns>
        public static string NthWord(this string original, int word)
        {
            return original.Split(' ')[word];
        }

        /// <summary>
        /// Trims newline characters from the end of a string.
        /// </summary>
        /// <param name="original"></param>
        /// <returns>Trimmed string.</returns>
        public static string TrimNewline(this string original)
        {
            return original.TrimEnd(new char[] { '\r', '\n' });
        }

        public static string NandN(this string[] elements)
        {
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < elements.Length; i++)
            {
                if (i != elements.Length - 1)
                {
                    sb.Append(elements[i] + ", ");
                }
                else
                {
                    sb.Append("and " + elements[i]);
                }
            }
            return sb.ToString();
        }

        #endregion
    }
}
